/**
 * Created by meiyun1 on 2017/8/2.
 */
import { getToken, setToken, removeToken } from '../../utils/auth'
import * as types from '../mutation-types'
import authApi from '../../api/auth'
import _ from 'lodash'

const stateData = {
  token: getToken(),
  userData: null,
  permissions: []
}
const getters = {
  token: state => state.token,
  userData: state => state.userData,
  permissions: state => state.permissions
}
const mutations = {
  [types.SET_TOKEN] (state, data) {
    state.token = data
    setToken(data)
  },
  [types.SET_USERINFO] (state, data) {
    state.userData = data
  },
  [types.SET_USER_PERMISSIONS] (state, data) {
    state.permissions = data
  },
  [types.SAVE_USERINFO] (state, data) {
    localStorage.setItem('login_info', JSON.stringify(data))
  }
}
const actions = {
  doLogin ({commit}, loginData) {
    // const username = loginData.username.trim()
    return new Promise((resolve, reject) => {

      // commit(types.SET_TOKEN, 'dasdasdgdfgg')
      //
      // resolve()

      authApi.doLogin(loginData.username, loginData.password).then(response => {

        if (loginData.save) {
          commit(types.SAVE_USERINFO, {
            'username': loginData.username,'password': loginData.password
          })
        }

        commit(types.SET_TOKEN, response.data.access_token)

        resolve(response)

      }, response => {
        reject(response)
      })

    })
  },
  getMyProfile ({commit},token) {

    return new Promise((resolve, reject) => {

      /**
       * 设置用户的权限
       */
      //commit(types.SET_USER_PERMISSIONS, ['all'])

      /**
       * 设置用户的信息
       */
      // commit(types.SET_USERINFO, {
      //   id: 1,
      //   name: '发送方',
      //   email: '5665@aa.com',
      //   mobile: '18788888888',
      //   avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80'
      // })
      //
      // resolve()

      authApi.getMyProfile(token).then(response => {

        const result = response.data;

        const permissions =['all']; //_.union(_.map(_.flatten(_.map(result.data.roles.data, 'permissions.data')), 'name'))

        commit(types.SET_USER_PERMISSIONS, permissions)

        commit(types.SET_USERINFO, {
          id: result.id,
          name: result.name,
          email: result.email ||'4041899282qq.com',
          mobile: result.mobile || '15184406336',
          nickname: result.nickname || 'yangxu',
          avatar: result.imgUrl || 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80',
          token:token,
          company:result.company||'成都阳帆网络科技',
          role:result.role
        });

        resolve(response)
      }, response => {

        reject(response)

      })

    })
  },
  loginOut ({commit,state,rootState}) {

    rootState.loading=!rootState.loading;

    return new Promise((resolve, reject) => {

      authApi.loginOut(state.token).then(response => {
        removeToken()
        commit(types.SET_TOKEN, '')
        commit(types.SET_USERINFO, {})
        commit(types.SET_USER_PERMISSIONS, [])
        commit(types.CLEAR_VISITVIEW)

        resolve(response)

        rootState.loading=!rootState.loading;

      }, response => {
        removeToken()

        commit(types.SET_TOKEN, '')
        commit(types.SET_USERINFO, {})
        commit(types.SET_USER_PERMISSIONS, [])
        commit(types.CLEAR_VISITVIEW)

        reject(response)

        rootState.loading =!rootState.loading;
      })

      // removeToken()
      // commit(types.SET_TOKEN, '')
      // commit(types.SET_USERINFO, {})
      // commit(types.SET_USER_PERMISSIONS, [])
      // commit(types.CLEAR_VISITVIEW)
      //
      // resolve();

      //reject()

    })
  }
}

export default {
  state: stateData,
  getters,
  mutations,
  actions
}
